package com.umeox.calculator;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.res.Resources;
import android.text.Editable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ActionMode;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.EditText;
import android.widget.Toast;
import com.google.common.collect.ImmutableMap;

public class CalculatorEditText extends EditText {
    private String[] mMenuItemsStrings;
    private String[] sOperators;
    private ImmutableMap<String, String> sReplacementTable;

    public CalculatorEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomSelectionActionModeCallback(new NoTextSelectionMode());
        setInputType(524289);
    }

    public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
        super.onInitializeAccessibilityEvent(event);
        String mathText = mathParse(getText().toString());
        if (!TextUtils.isEmpty(mathText)) {
            event.getText().clear();
            event.getText().add(mathText);
            setContentDescription(mathText);
        }
    }

    public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
        super.onInitializeAccessibilityNodeInfo(info);
        info.setText(mathParse(getText().toString()));
    }

    public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
        super.onPopulateAccessibilityEvent(event);
    }

    private String mathParse(String plainText) {
        String parsedText = plainText;
        if (!TextUtils.isEmpty(parsedText)) {
            initializeReplacementTable();
            String[] arr$ = this.sOperators;
            for (String operator : arr$) {
                if (this.sReplacementTable.containsKey(operator)) {
                    parsedText = parsedText.replace(operator, this.sReplacementTable.get(operator));
                }
            }
        }
        return parsedText;
    }

    private synchronized void initializeReplacementTable() {
        if (this.sReplacementTable == null) {
            ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
            Resources res = getContext().getResources();
            this.sOperators = res.getStringArray(R.array.operators);
            String[] descs = res.getStringArray(R.array.operatorDescs);
            int pos = 0;
            for (String key : this.sOperators) {
                builder.put(key, descs[pos]);
                pos++;
            }
            this.sReplacementTable = builder.build();
        }
    }

    private class MenuHandler implements MenuItem.OnMenuItemClickListener {
        private MenuHandler() {
        }

        public boolean onMenuItemClick(MenuItem item) {
            return CalculatorEditText.this.onTextContextMenuItem(item.getTitle());
        }
    }

    public boolean onTextContextMenuItem(CharSequence title) {
        if (TextUtils.equals(title, this.mMenuItemsStrings[0])) {
            cutContent();
            return true;
        } else if (TextUtils.equals(title, this.mMenuItemsStrings[1])) {
            copyContent();
            return true;
        } else if (!TextUtils.equals(title, this.mMenuItemsStrings[2])) {
            return false;
        } else {
            pasteContent();
            return true;
        }
    }

    public void onCreateContextMenu(ContextMenu menu) {
        MenuHandler handler = new MenuHandler();
        if (this.mMenuItemsStrings == null) {
            Resources resources = getResources();
            this.mMenuItemsStrings = new String[3];
            this.mMenuItemsStrings[0] = resources.getString(17039363);
            this.mMenuItemsStrings[1] = resources.getString(17039361);
            this.mMenuItemsStrings[2] = resources.getString(17039371);
        }
        for (int i = 0; i < this.mMenuItemsStrings.length; i++) {
            menu.add(0, i, i, this.mMenuItemsStrings[i]).setOnMenuItemClickListener(handler);
        }
        if (getText().length() == 0) {
            menu.getItem(0).setVisible(false);
            menu.getItem(1).setVisible(false);
        }
        ClipData primaryClip = getPrimaryClip();
        if (primaryClip == null || primaryClip.getItemCount() == 0 || !canPaste(primaryClip.getItemAt(0).coerceToText(getContext()))) {
            menu.getItem(2).setVisible(false);
        }
    }

    private void setPrimaryClip(ClipData clip) {
        ((ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE)).setPrimaryClip(clip);
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void copyContent() {
        Editable text = getText();
        int textLength = text.length();
        setSelection(0, textLength);
        ((ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE)).setPrimaryClip(ClipData.newPlainText(null, text));
        Toast.makeText(getContext(), (int) R.string.text_copied_toast, Toast.LENGTH_SHORT).show();
        setSelection(textLength);
    }

    private void cutContent() {
        Editable text = getText();
        int textLength = text.length();
        setSelection(0, textLength);
        setPrimaryClip(ClipData.newPlainText(null, text));
        getText().delete(0, textLength);
        setSelection(0);
    }

    private ClipData getPrimaryClip() {
        return ((ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE)).getPrimaryClip();
    }

    private void pasteContent() {
        ClipData clip = getPrimaryClip();
        if (clip != null) {
            for (int i = 0; i < clip.getItemCount(); i++) {
                CharSequence paste = clip.getItemAt(i).coerceToText(getContext());
                if (canPaste(paste)) {
                    getText().insert(getSelectionEnd(), paste);
                }
            }
        }
    }

    private boolean canPaste(CharSequence paste) {
        try {
            Float.parseFloat(paste.toString());
            return true;
        } catch (NumberFormatException e) {
            Log.e("Calculator2", "Error turning string to integer. Ignoring paste.", e);
            return false;
        }
    }

    class NoTextSelectionMode implements ActionMode.Callback {
        NoTextSelectionMode() {
        }

        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            return false;
        }

        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            CalculatorEditText.this.copyContent();
            return false;
        }

        public void onDestroyActionMode(ActionMode mode) {
        }

        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }
    }
}
